package org.example.groupwarehouse.controller;

import org.example.groupwarehouse.service.MinioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;
// AdminFileController 是一个基于 Spring Boot 的文件管理控制器
// ，主要用于处理管理员后台的文件上传和删除操作，结合 MinIO 对象存储服务实现文件管理。
@Controller
@RequestMapping("/admin/file")
public class AdminFileController {

    @Autowired
    private MinioService minioService;

    // 通用文件上传接口
    @PostMapping("/upload")
    @ResponseBody
    public Map<String, String> uploadFile(@RequestParam("file") MultipartFile file) {
        Map<String, String> result = new HashMap<>();
        try {
            String url = minioService.uploadFile(file);
            result.put("status", "success");
            result.put("url", url);
        } catch (Exception e) {
            result.put("status", "error");
            result.put("msg", e.getMessage());
        }
        return result;
    }

    // 文件删除接口
    @PostMapping("/delete")
    @ResponseBody
    public Map<String, String> deleteFile(@RequestParam("url") String fileUrl) {
        Map<String, String> result = new HashMap<>();
        try {
            minioService.deleteFile(fileUrl);
            result.put("status", "success");
        } catch (Exception e) {
            result.put("status", "error");
            result.put("msg", e.getMessage());
        }
        return result;
    }
}